package com.vortex.cloud.sample.manager;

import com.google.common.collect.Maps;
import com.vortex.cloud.sample.dto.DictDataDTO;
import com.vortex.cloud.sdk.api.service.IUmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-10-14 周四
 * @time 16:00
 * @description
 */
@Service
public class UmsManagerService {
    @Autowired
    private IUmsService umsService;

    public static final String SDK_OBJECT_CODE_PARAM_SETTING = "PARAM_SETTING";
    public static final String SDK_OBJECT_CODE_DIVISION = "DIVISION";
    public static final String SDK_OBJECT_CODE_DEPT_ORG = "DEPT_ORG";
    public static final String SDK_OBJECT_CODE_STAFF = "STAFF";
    public static final String SDK_OBJECT_CODE_USER = "USER";

    public Map<String, DictDataDTO> mapDictData(String sdkObjectCode, String tenantId, String parameters) {
        switch (sdkObjectCode) {
            case SDK_OBJECT_CODE_PARAM_SETTING:
                return umsService.getByParamTypeCode(tenantId, parameters)
                        .stream()
                        .map(r -> new DictDataDTO(r.getParmCode(), r.getParmName()))
                        .collect(Collectors.toMap(DictDataDTO::getKey, r -> r, (r1, r2) -> r1));
            case SDK_OBJECT_CODE_DIVISION:
                return umsService.getDivisionList(tenantId, true, null, null)
                        .stream()
                        .map(r -> new DictDataDTO(r.getId(), r.getName()))
                        .collect(Collectors.toMap(DictDataDTO::getKey, Function.identity()));
            case SDK_OBJECT_CODE_DEPT_ORG:
                return umsService.loadDepartments(tenantId)
                        .stream()
                        .map(r -> new DictDataDTO(r.getId(), r.getText()))
                        .collect(Collectors.toMap(DictDataDTO::getKey, Function.identity()));
            case SDK_OBJECT_CODE_STAFF:
                return umsService.loadStaffsByFilter(tenantId)
                        .stream()
                        .map(r -> new DictDataDTO(r.getId(), r.getName()))
                        .collect(Collectors.toMap(DictDataDTO::getKey, Function.identity()));
            case SDK_OBJECT_CODE_USER:
                return umsService.getusersbycondiction(tenantId)
                        .stream()
                        .map(r -> new DictDataDTO(r.getId(), r.getStaffName()))
                        .collect(Collectors.toMap(DictDataDTO::getKey, Function.identity()));
            default:
                return Maps.newHashMap();
        }
    }
}
